using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEngine;

/**
 *Copyright(C) 2019 by 殊未
 *All rights reserved.
 *FileName:     
 *Author:      丁冉
 *Version:      BVP3.0
 *UnityVersion:    2018.4.0
 *Date:         2020-04-23
 *Description:   
 *History:
*/

public static class MatchSelf
{
    #region 测试信息
    ////// Start is called before the first frame update
    //void Start()
    //{
    //    //"?(?<town>[^区]+区|.+镇)?"
    //    string add3 = "上海市**上海市**闵行区**华宁路2931号 中船重工第七0一研究所上海分部";
    //    //string add3 = "江苏省 苏州市 相城区 太平街道 不要给我放菜鸟驿站！苏州市相城区太平镇青漪花园24幢（放西门卫）";
    //    //string add3 = "江西省景德镇市乐平市武装部慧武楼门卫处";
    //    //string add3 = "深圳市龙岗区南湾街道宝冠路恩来工业城，周六日照常派送";

    //    SplitStrInfo(add3);
    //} 
    #endregion
    public static string SplitStr = "";
    public static int CountyIndex = 0;
    /// <summary>
    /// 获取城市信息
    /// </summary>
    /// <param name="address"></param>
    /// <returns></returns>
    public static SplitInfo SplitStrInfo(string address)
    {
        #region 未分段识别
        ////string regex = "(?<province>[^省]+省|.+自治区|上海|北京|天津|重庆|上海市|北京市|天津市|重庆市)(?<city>[^市]+市|.+自治州)(?<county>[^县]+县|[^区].+区|.+镇|.+局)(?<village>.*)";
        ////string regex = "(?<province>[^省]+省|.+自治区|上海|北京|天津|重庆|上海市|北京市|天津市|重庆市)(?<city>[^市]+市|.+自治州)(?<county>.*?区|[^县]+县|[^区].+区|.+镇|.+局)?(?<town>[^区]+区|.+镇)?(?<village>.*)";
        //string regex = "(?<province>[^省]+省|.+自治区|上海|北京|天津|重庆|上海市|北京市|天津市|重庆市)(?<city>[^市]+市|.+自治州)(?<county>.*?区|[^县]+县|[^区].+区|.+镇|.+局)(?<village>.*)";
        //Regex r = new Regex(regex);
        //Match m = r.Match(address);

        //GroupCollection group = m.Groups;
        //string province = group["province"].Value;
        //string city = group["city"].Value;
        //string county = group["county"].Value;
        //string village = group["village"].Value;

        //return new SplitInfo(province, city, county, village); 
        #endregion
        Initialized();
        string province = SplitStrProvince(address);
        string city = SplitStrCity();
        string county = SplitStrCounty();
        string village = SplitStrVillage();


        SplitInfo splitInfo = new SplitInfo(province, city, county, village);
        //Debug.Log(splitInfo.ToString());
        return splitInfo;
    }

    /// <summary>
    /// 初始化
    /// </summary>
    private static void Initialized()
    {
        SplitStr = "";
        CountyIndex = 0;
    }


    /// <summary>
    /// 获取省
    /// </summary>
    /// <param name="address"></param>
    /// <returns></returns>
    public static string SplitStrProvince(string address)
    {
        string regex = "(?<province>[^省]+省|.+自治区|上海市|北京市|天津市|重庆市|上海|北京|天津|重庆)";
        Regex r = new Regex(regex);
        Match m = r.Match(address);

        GroupCollection group = m.Groups;

        Capture capture = group["province"];
        string province = "";

        province = capture.Value;
        if (string.IsNullOrEmpty(province))
        {
            SplitStr = address;
        }
        else
        {
            SplitStr = address.Substring(capture.Length + capture.Index);
        }
        return province;
    }

    /// <summary>
    /// 获取市
    /// </summary>
    /// <param name="address"></param>
    /// <returns></returns>
    public static string SplitStrCity()
    {
        string regex = "(?<city>[^市]+市|.+自治州)";
        Regex r = new Regex(regex);
        Match m = r.Match(SplitStr);

        GroupCollection group = m.Groups;
        Capture capture = group["city"];

        string city = "";
        city = capture.Value;
        if (!string.IsNullOrEmpty(city))
        {
            SplitStr = SplitStr.Substring(capture.Length + capture.Index);
        }
        return city;
    }

    /// <summary>
    /// 获取区
    /// </summary>
    /// <param name="address"></param>
    /// <returns></returns>
    public static string SplitStrCounty()
    {
        string regex = "(?<county>.*?区|[^县]+县|[^区].+区|.+镇|.+局)";
        Regex r = new Regex(regex);
        Match m = r.Match(SplitStr);
        GroupCollection group = m.Groups;
        Capture capture = group["county"];
        string county = "";
        county = group["county"].Value;

        if (!string.IsNullOrEmpty(county))
        {
            SplitStr = SplitStr.Substring(capture.Length + capture.Index);
        }

        CountyIndex = capture.Index;

        return county;
    }


    public static string SplitStrVillage()
    {
        string village = "";
        village = SplitStr.Substring(CountyIndex);
        return village;
    }

}
public class SplitInfo
{
    public string Province { get; set; }
    public string City { get; set; }
    public string County { get; set; }
    public string Village { get; set; }

    public SplitInfo(string province, string city, string county, string village)
    {
        Province = province;
        City = city;
        County = county;
        Village = village;
    }

    public override string ToString()
    {
        return string.Format("省: {0},市: {1},区: {2},地址: {3}", Province, City, County, Village);
    }
}
